<template>
	<div class="box">
		<u-form size="medium" ref="expressForm" :model="order" label-width="100px">
			<u-form-item label="收货人">{{ order.user_real_name }}</u-form-item>
			<u-form-item label="联系电话">{{ order.user_mobile }}</u-form-item>
			<u-form-item label="收货地址">{{ order.address }}</u-form-item>
			<u-form-item label="配送方式" @click="show = true">
				<u-radio-group @change="changetype" v-model="express_type">
					<u-radio v-for="(item, i) in thirdlist" :name="item.value" :label="item.label"></u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="第三方运费" v-if="express_type == 1">
				<span>￥{{ fee }}</span>
			</u-form-item>
			<u-form-item v-if="status == 0">
				<span>{{ msg }}</span>
			</u-form-item>
			<!-- <u-form-item><el-button v-if="status == 1" type="primary" @click="saveData">确定发货</el-button></u-form-item> -->
		</u-form>
		<view class="u-flex u-flex-items-center footer">
			<u-button size="small" shape="circle" @click="close">取消</u-button>
			<u-button class="u-m-l-60" type="primary" size="small" shape="circle" @click="saveData">确定</u-button>
		</view>
	</div>
</template>
<script>
export default {
	name: 'orderExpress',
	data() {
		return {
			show: false,
			isLoading: false,
			expressList: [],
			disabled: false,
			deliver_type: 1,
			status: 1,
			thirdlist: [
			
			],
			fee: 0,
			express_type: 0,
			itemRules: {
				express_company: [
					{
						required: true,
						message: '请选择物流'
					}
				],
				logistics_no: [
					{
						required: true,
						message: '请输入单号'
					}
				]
			},
			cityCode: ''
		};
	},
	props: {
		order: {
			type: Object,
			default: function() {
				return {};
			}
		}
	},
	created() {
		this.getDistribution();
		
	},
	computed: {
		comm_select_length: function() {
			return this.order.orderCommoditys.filter(_ => _.status == '待发货').length;
		}
	},
	methods: {
		getcityCode() {
			let data = {oid: this.order.id};
			uni.$u.http.get('/shop/dada/cityCode', {params: data, custom: {id: 8}}).then(res => {
				let list = res.msg.result;
				let address = this.order.address.split(',');
				if (list && list.length > 0) {
					list.forEach(a => {
						if (this.order.address.indexOf(a.cityName) > -1) {
							this.cityCode = a.cityCode;
						}
					});
				}
				this.changetype(1);
				
		
			});
		},
		//选择门店自动激活选中当前记录
		selItem(i) {
			this.order.orderCommoditys[i].isSelected = true;
		},
		changetype(e) {
			if (e == 1) {
				let _data = {};
				_data.oid = this.order.id;
				_data.type = this.express_type == 0 ? 1 : 2;
				if (this.express_type > 0) {
					_data.third = this.express_type;
					_data.data = {city_code: this.cityCode, is_prepay: 0, cargo_weight: 5};
				}

				this.isLoading = true;
				uni.$u.http.post('/shop/order/localDelivery', _data,{custom:{id:8}}).then(res => {
					let data = res.msg;
					// let json = data[0];
					this.fee = data.fee;
					this.expressinfo = data;
					if (data.status) {
						this.status = 1;
					} else {
						this.msg = data.msg;
						this.status = 0;
					}
				});
			}
		},
		//获取配送方式
		getDistribution() {
			this.isLoading = true;
			uni.$u.http.get('/shop/order/getDistribution', {params: {oid: this.order.id},
			custom:{id:8}
			}).then(res => {
				this.isLoading = false;
				let list = res.msg;
				let thirdlist = [];

				list.forEach(a => {
					if (a.third) {
						if (a.third.indexOf('2') > -1) {
							thirdlist.push({
								label: '达达',
								value: 1
							});
						}
						if (a.third.indexOf('1') > -1) {
							thirdlist.push({
								label: '美团',
								value: 2
							});
						}
					} else {
						thirdlist.push({
							label: '商家配送',
							value: 0
						});
					}
				});

				this.thirdlist = thirdlist.sort((a, b) => {
				  return a.value - b.value;
				});
				this.express_type = this.thirdlist[0].value;
				if (this.express_type == 1) {
				  this.getcityCode();
				}
		
				// if(list.findIndex(_=>_=='1')>-1){

				// }
				this.$refs.expressForm.setRules(this.itemRules);
				return false;
				this.expressList = res.msg;
			});
		},

		//保存数据(发货)
		saveData() {
			this.$refs.expressForm.validate().then(res => {
				let _data = {};
				_data.oid = this.order.id;
				_data.type = this.express_type == 0 ? 1 : 2;
				if (this.express_type > 0) {
					_data.third = this.express_type;
				}

				this.isLoading = true;
				if (this.express_type == 0) {
					uni.$u.http
						.post('/shop/order/localDelivery', _data,{custom:{id:8}})
						.then(res => {
							if (this.express_type == 0) {
								this.isLoading = false;
								this.$emit('success');
							} else {
								// this.dadaPay(res.msg[1]);
							}
						})
						.catch(e => {
							this.isLoading = false;
						});
				} else {
					this.dadaPay();
				}
			});
		},
		dadaPay() {
			let params = {
				id: this.expressinfo.id,
				oid: this.expressinfo.order_id
			};
			uni.$u.http.post('/shop/dada/pay', params,{custom:{id:8}}).then(res => {
				this.isLoading = false;
				this.$emit('success');
			});
		},
		close() {
			this.$emit('close', 'showLocal');
		}
	}
};
</script>

<style lang="scss" scoped>
.u-form-item {
	margin-bottom: 15px;
}

//选择商品
.proSelect {
	width: 100%;
	border: 1px solid #dcdfe6;
	//margin-bottom: 10px;

	.phead {
		background: #f4f6f8;
		font-weight: 700;
		display: flex;
		height: 40px;
		line-height: 40px;
		border-bottom: 1px solid #dcdfe6;

		.name {
			flex: 1;
			padding-left: 10px;
		}

		.store {
			width: 180px;
			text-align: center;
		}

		.count {
			width: 80px;
			text-align: center;
		}

		.status {
			width: 80px;
			text-align: center;
		}

		.action {
			width: 100px;
			text-align: center;
		}
	}

	ul {
		margin: 0;
		max-height: 210px;
		overflow-y: auto;

		li {
			display: flex;
			cursor: pointer;
			line-height: 40px;
			padding: 10px 0;
			font-size: 13px;
			border-bottom: 1px solid #dcdfe6;
			padding-left: 10px;

			&:last-child {
				border-bottom: none;
			}

			.pro {
				display: flex;
				flex: 1;

				.txt {
					display: flex;
					padding-left: 10px;
					flex: 1;
					line-height: 18px;
					align-items: center;

					.mark {
						color: #f56c6c;
						background: #f2f4f6;
						display: table;
						border-radius: 5px;
						padding: 0 3px;
						margin-right: 3px;
					}
				}
			}

			.store {
				width: 180px;
				text-align: center;
			}

			.count {
				width: 80px;
				text-align: center;
			}

			.status {
				width: 80px;
				text-align: center;

				&.done {
					color: #67c23a;
				}

				&.none {
					color: #f56c6c;
				}
			}

			.action {
				width: 100px;
				text-align: center;
			}
		}
	}
}

.el-input {
	width: 270px;
}

.box {
	width: 100%;
	/*padding: 60px;
  height: calc(100vh - 77px);
  overflow: scroll;*/
}
</style>
